﻿@using PearAdmin.AbpTemplate.Admin.Views
@using PearAdmin.AbpTemplate.Authorization
@{
    ViewBag.CurrentPageName = AbpTemplatePageName.Users;
}

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form form-search" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="realName" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_SystemManagement_Users_Create))
    {
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>新增
        </button>
    }
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_SystemManagement_Users_Delete))
    {
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    }
</script>

<script type="text/html" id="user-bar">
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_SystemManagement_Users_Update))
    {
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    }
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_SystemManagement_Users_Delete))
    {
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
    }
    @if (await PermissionChecker.IsGrantedAsync(AppPermissionNames.Pages_SystemManagement_Users_ResetPassword))
    {
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="resetPassword"><i class="layui-icon layui-icon-refresh"></i></button>
    }
</script>

<!--角色拼接-->
<script type="text/javascript">
    var composeRoleNames = function (arr) {
        var sum = '';
        for (var i = 0; i <= arr.length - 1; i++) {
            sum += arr[i];
            if (i < arr.length - 1) {
                sum += '、';
            }
        }

        return sum;
    };
</script>

<script type="text/html" id="user-roles">
	{{ composeRoleNames(d.roleNames) }}
</script>

@section scripts{
    <script type="text/javascript">
        layui.use(['abp', 'form', 'table'], function () {
            var $ = layui.jquery;
            var abp = layui.abp;
            var form = layui.form;
            var table = layui.table;
		
		    table.render({
		        elem: '#user-table',
                url: '@Url.Action("GetUserList", "Users")',
                height: 'full-150',
                page: true,
                parseData: function (res) {
                    return {
                        "code": res.result.code, //解析接口状态
                        "msg": res.result.msg, //解析提示文本
                        "count": res.result.count, //解析数据长度
                        "data": res.result.data //解析数据列表
                    };
                },
                response: {
                    statusCode: 200 //规定成功的状态码，默认：0
                },
		        cols: [
		            [
		                {type:'checkbox'},
		                {title: '用户名', field: 'userName', align:'center', width:100},
		                {title: '手机号', field: 'phoneNumber', align:'center'},
		                {title: '角色', field: 'roleNames', align:'center',templet:'#user-roles'},
		                {title: '操作', toolbar: '#user-bar', align:'center', width:180}
		            ]
		        ],
		        skin: 'line',
		        toolbar: '#user-toolbar',
		        defaultToolbar: [{
		            layEvent: 'refresh',
		            icon: 'layui-icon-refresh',
		        }, 'filter', 'print', 'exports']
		    });
		
            table.on('tool(user-table)', function (obj) {
                var data = obj.data;
                if (obj.event === 'remove') {
                    window.remove([{ "id": data.id }]);
		        } else if(obj.event === 'edit'){
		            window.edit(data);
		        }
		    });
		
		    table.on('toolbar(user-table)', function(obj){
		        if(obj.event === 'add'){
		            window.add();
		        } else if(obj.event === 'refresh'){
		            window.refresh();
                } else if (obj.event === 'batchRemove') {
                    let checkData = table.checkStatus(obj.config.id).data;
				    if (checkData.length === 0) {
					    parent.layer.msg("未选中数据", {
						    icon: 3,
						    time: 1000
					    });
					    return false;
                    }
                    var ids = checkData.map(function (d) { return { "id": d.id }; });
                    window.remove(ids);
		        }
		    });
		
		    form.on('submit(user-query)', function(data){
                table.reload('user-table', {
                    where: data.field,
					page: {
                        curr: 1
                    }
                })
		        return false;
            });

			window.saveCallback = function (data) {
                parent.layer.close(data.index);
                abp.notify.success(data.msg);
                table.reload("user-table");
            }

			window.add = function () {
				parent.layer.open({
                    type: 2,
                    title: "添加用户",
                    content: "@Url.Action("CreateOrUpdateUser", "Users")",
                    area: ["710px","436px"],
                    shade: 0.1,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe'+ index].submitForm();
                    }
                });
            }
		
		    window.edit = function(data){
		        parent.layer.open({
                    type: 2,
                    title: "编辑用户",
                    content: "@Url.Action("CreateOrUpdateUser", "Users")" + abp.utils.formatString("?id={0}", data.id),
                    area: ["710px","436px"],
                    shade: 0.1,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe'+ index].submitForm();
                    }
                });
		    }
		
            window.remove = function (data) {
                parent.layer.confirm('确定删除吗?', {
					icon: 3,
					title: '提示'
                }, function (index) {
                    parent.layer.close(index);
					let waitIndex = parent.layer.load(2);
                    abp.ajax({
                        url: "@Url.Action("DeleteUser", "Users")",
                        data: JSON.stringify(data),
						abpHandleError: false
                    }).done(function (data) {
                        if (data.code == 200) {
                            abp.notify.success(data.msg);
                            table.reload('user-table');
                        }
                    }).fail(function (jqXHR) {
                        parent.layer.msg(jqXHR.message, { icon: 5 });
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
		    }
		
		    window.refresh = function(param){
		        table.reload('user-table');
		    }
        });
    </script>
}